﻿Public Class UpdateDeleteShopOutlet
    Inherits System.Web.UI.Page

    Public DBManager As DBManager = New DBManager

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Try
                If (Request.QueryString("id") IsNot Nothing) Then
                    Dim fullID = Request.QueryString("id")

                    Dim type = fullID(0)


                    Select Case type
                        Case "b"
                            Dim splitIDString = fullID.Split("b")
                            Dim businessID = splitIDString(1)
                            lblBusinessName.Text = DBManager.getBusinessOutletName(businessID)
                            PanelOutlets.Visible = False
                            ddlReportBusiness.Visible = True
                            rfvBusiness.Enabled = True
                            ddlReportOutlet.Visible = False
                            rfvOutlet.Enabled = False

                        Case "o"
                            Dim splitIDString = fullID.Split("o")
                            Dim outletID = splitIDString(1)
                            lblBusinessName.Text = DBManager.getBusinessOutletName(outletID)
                            populateOutletList(outletID)
                            PanelOutlets.Visible = True
                            ddlReportOutlet.Visible = True
                            rfvOutlet.Enabled = True
                            ddlReportBusiness.Visible = False
                            rfvBusiness.Enabled = False
                    End Select

                Else
                    Response.Redirect("home.aspx")
                End If
            Catch ex As Exception
            End Try
        End If

        'Business name - outlet name
        'Dim businessOutletName = DBManager.getBusinessOutletName(outletID)
        'lblBusinessName.Text = businessOutletName
    End Sub

    'Populate the dropdownlist with all the outlets belonging to the business 
    Protected Sub populateOutletList(ByVal outletID As Integer)
        Dim itemList = DBManager.getListOutlets(outletID)
        For Each outlet As ListItem In itemList
            ddlOutlets.Items.Add(outlet)
        Next
    End Sub

    Protected Sub ButtonDone_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonDone.Click

        If (Request.QueryString("id") IsNot Nothing) Then
            Dim fullID = Request.QueryString("id")

            Dim type = fullID(0)
            Dim redirectID As Integer = 0
            Select Case type
                Case "b"
                    Dim splitIDString = fullID.Split("b")
                    Dim businessID = splitIDString(1)
                    addBusinessReport(businessID)
                    redirectID = DBManager.getOutletIDFromBusinessID(businessID)
                Case "o"
                    addOutletReport()
                    redirectID = ddlOutlets.SelectedValue
            End Select
            Response.Redirect("OutletReview.aspx?id=" + redirectID.ToString)
        Else
            MsgBox("Failed", MsgBoxStyle.OkOnly)
        End If
    End Sub

    Protected Sub addBusinessReport(ByVal businessID As Integer)
        ' deleting and updating priority ranking in the dashboard 
        Dim priority = 2
        Dim resolved = 0
        ' add to the database 
        DBManager.addOutletReport(priority, ddlReportBusiness.SelectedItem.Text, tbxComments.Text, businessID, resolved)
    End Sub

    Protected Sub addOutletReport()
        ' deleting and updating priority ranking in the dashboard 
        Dim priority = 2
        Dim resolved = 0
        ' add to the database 
        DBManager.addOutletReport(priority, ddlReportOutlet.SelectedItem.Text, tbxComments.Text, ddlOutlets.SelectedValue, resolved)
    End Sub

End Class